# controlls the creation, display and editing of projects.
# much of the UI configuration can be found here as the 
# majority of the applicaion centers around projects

class ProjectsController < ApplicationController
  layout 'admin_standard'
  before_filter :authenticate
  before_filter :get_project_context
  active_scaffold :project
  active_scaffold :project do |config|
      config.label = ''
      #config.show.link.page = true
      #config.show.link.action :show
      #config.show.type :record
      config.columns = [:name, :short_name, :status, :site_update_frequency, :teams, :bug_lists, :admins, 
                        :projection_start_date,  :projection_weeks, :projection_end_date,
                       :bug_tool_url]
      #config.columns[:teams].label = "Teams"
      config.columns[:active].label = "Active"
      config.columns[:projection_start_date].label = "Start Date"
      config.columns[:projection_weeks].label = "Duration"
      config.columns[:projection_end_date].label = "End Date"

      config.update.columns = [:name, :active, :site_update_frequency, 
                        :projection_start_date,  :projection_weeks, 
                       :bug_tool_url]
      config.create.columns = [:name, :short_name, :active, :site_update_frequency, 
                              :projection_start_date,  :projection_weeks, 
                              :bug_tool_url]
      update.columns.exclude :teams, :bug_lists, :admins, :projection_end_date, :edit_control
      create.columns.exclude :teams, :bug_lists, :admins, :projection_end_date, :active
      #config.update.columns[:projection_weeks].label = "Duration (in weeks)"
      config.columns[:site_update_frequency].form_ui = :select 
      config.columns[:site_update_frequency].label = "Update Frequency" 
      config.columns[:bug_tool_url].form_ui = :select
      #show.link_actions.exclude :create
      config.actions.exclude :delete, :show
      
      
      #update.columns.add :table_column_order
      list.sorting = {:site_update_frequency => :desc}

    end


    
  ##
  # used as a before filter to make sure the UI has access to the project
  def get_project_context
    if( !params[:id].nil? )
      @project = Project.find(params[:id])
    end
  end 
  

  
  # GET /projects
  # GET /projects.xml
  def index
    @projects = Project.find(:all)


    respond_to do |format|
      format.html { render :active_scaffold => @projects }# index.html.erb
      format.xml  { render :xml => @projects }
    end
  end

  # POST /projects
  # POST /projects.xml
  def before_create_save(record)
      record.active = false
  end

  ##
  # triggered after a project save.
  # Adds the current logged in user as an admin and
  # creates the generated site directory structure for the site
  def after_create_save(record)
    # write out the properties so they show up for the generators
    
    ## give this new project an adminstrator
    admin = Person.find_or_create_by_username(session[:username])
    administrator = Administrator.new
    administrator.person = admin
    administrator.project = record
    administrator.save
    
    ## create the site directories
    record.create_site_directory_structure
    record.publish_site_properties 
  end

  # GET /projects/1
  # GET /projects/1.xml
  def after_update_save(record)
    # write out the properties so they show up for the generators
    record.publish_site_properties
  end


  def show
    @project = nil
    
    if !params[:site].nil?
      @project = Project.find_by_short_name(params[:site])
    else
      @project = Project.find(params[:id])
    end
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @project }
    end

  end

end

